const { Configuration, DefinePlugin } = require('webpack')
const path = require('node:path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

/**
 * @type { Configuration }
 */

const config = {
    mode: "development",
    entry: './src/main.ts',
    output: {
        path: path.resolve(__dirname, "dist"),//生成dist目录
        filename: '[chunkhash].js',
        clean: true
    },
    stats: "errors-only",
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: {
                    loader: 'ts-loader',
                    options: {
                        appendTsSuffixTo: [/\.vue$/]
                    }
                }
            },
            {
                test: /\.vue$/,
                use: {
                    loader: 'vue-loader'
                }
            },
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader']
            },
            {
                test: /\.scss$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            }
        ]
    },
    plugins: [
        new HTMLWebpackPlugin({
            template: './index.html'
        }),
        new VueLoaderPlugin(),
        new MiniCssExtractPlugin(),
        new DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
            '__DEV__': JSON.stringify(process.env.NODE_ENV !== 'production'),
            '__PROD__': JSON.stringify(process.env.NODE_ENV === 'production'),
            '__VERSION__': JSON.stringify(require('./package.json').version)
        })
    ],
    resolve: {
        extensions: ['.ts', '.js', '.vue', '.json'],
        alias: {
            '@': path.resolve(__dirname, 'src') // 确保这里正确指向 src 目录
        }
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                moment: {
                    name: 'moment',
                    chunks: 'all',
                    test: /[\\/]node_modules[\\/]moment[\\/]/,/*匹配moment库*/
                },
                common: {
                    name: "common",
                    chunks: "all",
                    minChunks: 2
                }
            }
        }
    }
}

module.exports = config